﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ArchPostprocessing.CompoundStructureManage.DataDefinition;
using ArchPostprocessing.CompoundStructureManage.Managers;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace ArchPostprocessing.CompoundStructureManage.Forms
{
	public partial class WallTypeEditForm : System.Windows.Forms.Form
	{
		public WallTypeEditForm(List<List<WallType>> wallGroups, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.m_Doc = doc;
			this.InitializeWallGroupInfoList(wallGroups);
			this.InitializeRemovedWallTypeList();
		}

		public List<WallTypeGroupInfos> GetCombinationInfosByGroup()
		{
			List<WallTypeGroupInfos> list = new List<WallTypeGroupInfos>();
			for (int i = 0; i < this.m_WallTypeGroupInfoList.Count; i++)
			{
				WallTypeGroupInfos wallTypeGroupInfos = this.m_WallTypeGroupInfoList[i].DeepClone();
				List<WallTypeLayerInfos> layerTypeList = wallTypeGroupInfos.LayerTypeList;
				foreach (int index in this.m_RemovedWallTypeList[i])
				{
					layerTypeList.Insert(index, null);
				}
				wallTypeGroupInfos.LayerTypeList = layerTypeList;
				list.Add(wallTypeGroupInfos);
			}
			return list;
		}

		private void WallTypeEditForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.UpdateUserSelectTreeView(0, 0);
		}

		private void InitializeRemovedWallTypeList()
		{
			this.m_RemovedWallTypeList.Clear();
			foreach (WallTypeGroupInfos wallTypeGroupInfos in this.m_OldWallTypeGroupInfoList)
			{
				List<int> item = new List<int>();
				this.m_RemovedWallTypeList.Add(item);
			}
		}

		private void AddToRemovedWallTypeList(int groupIndex, int typeIndex)
		{
			List<int> list = this.m_RemovedWallTypeList[groupIndex];
			for (int i = 0; i < list.Count; i++)
			{
				if (typeIndex < list[i])
				{
					this.m_RemovedWallTypeList[groupIndex].Insert(i, typeIndex);
					return;
				}
				typeIndex++;
			}
			this.m_RemovedWallTypeList[groupIndex].Add(typeIndex);
		}

		private void InitializeWallGroupInfoList(List<List<WallType>> wallGroups)
		{
			foreach (List<WallType> userSelectTypeList in wallGroups)
			{
				this.InitializeWallLayersInfoList(userSelectTypeList);
			}
		}

		private void InitializeWallLayersInfoList(List<WallType> userSelectTypeList)
		{
			List<WallTypeLayerInfos> list = new List<WallTypeLayerInfos>();
			foreach (WallType wallType in userSelectTypeList)
			{
				WallTypeLayerInfos wallLayerInfoListByType = WallTypeManager.GetWallLayerInfoListByType(wallType, this.m_Doc);
				list.Add(wallLayerInfoListByType);
			}
			WallTypeGroupInfos wallTypeGroupInfos = new WallTypeGroupInfos("组" + (this.m_WallTypeGroupInfoList.Count + 1).ToString(), list);
			this.m_WallTypeGroupInfoList.Add(wallTypeGroupInfos);
			WallTypeGroupInfos item = wallTypeGroupInfos.DeepClone();
			this.m_OldWallTypeGroupInfoList.Add(item);
		}

		private void UpdateUserSelectTreeView(int groupIndex, int typeIndex)
		{
			this.treeViewUserSelect.Nodes.Clear();
			for (int i = 0; i < this.m_WallTypeGroupInfoList.Count; i++)
			{
				TreeNode treeNode = new TreeNode(this.m_WallTypeGroupInfoList[i].GroupName);
				treeNode.Tag = i;
				this.treeViewUserSelect.Nodes.Add(treeNode);
				for (int j = 0; j < this.m_WallTypeGroupInfoList[i].LayerTypeList.Count; j++)
				{
					WallTypeLayerInfos wallTypeLayerInfos = this.m_WallTypeGroupInfoList[i].LayerTypeList[j];
					this.UpdateViewTypeCompoundStructure(wallTypeLayerInfos, j, treeNode);
				}
				treeNode.Expand();
			}
			this.treeViewUserSelect.SelectedNode = this.treeViewUserSelect.Nodes[groupIndex].Nodes[typeIndex];
		}

		private void UpdateViewTypeCompoundStructure(WallTypeLayerInfos wallTypeLayerInfos, int index, TreeNode parentNode)
		{
			TreeNode treeNode = new TreeNode(wallTypeLayerInfos.TypeName);
			treeNode.Tag = index;
			parentNode.Nodes.Add(treeNode);
		}

		private void treeViewUserSelect_AfterSelect(object sender, TreeViewEventArgs e)
		{
			int num = -1;
			int num2 = -1;
			if (!this.SelectedNodeIndices(ref num, ref num2))
			{
				return;
			}
			WallTypeGroupInfos wallTypeGroupInfos = this.m_WallTypeGroupInfoList[num];
			if (this.m_CommonForm != null)
			{
				if (this.m_CurrentTypeIndex != -1)
				{
					string typeName = this.m_WallTypeGroupInfoList[this.m_CurrentGroupIndex].LayerTypeList[this.m_CurrentTypeIndex].TypeName;
					for (int i = 0; i < this.m_WallTypeGroupInfoList[this.m_CurrentGroupIndex].LayerTypeList.Count; i++)
					{
						WallTypeLayerInfos wallTypeLayerInfos = this.m_WallTypeGroupInfoList[this.m_CurrentGroupIndex].LayerTypeList[i];
						if (typeName.CompareTo(wallTypeLayerInfos.TypeName) == 0)
						{
							this.m_WallTypeGroupInfoList[this.m_CurrentGroupIndex].LayerTypeList[i].LayerList = this.m_CommonForm.CurrentWallLayerInfo;
						}
					}
				}
				List<WallLayerInfos> layerList = wallTypeGroupInfos.LayerTypeList[num2].LayerList;
				this.m_CommonForm.CurrentWallLayerInfo = layerList;
			}
			else
			{
				List<WallLayerInfos> layerList2 = wallTypeGroupInfos.LayerTypeList[num2].LayerList;
				this.m_CommonForm = new CompoundStructureCommonForm(base.Handle, this.m_Doc, layerList2);
				this.m_CommonForm.TopLevel = false;
				this.m_CommonForm.Parent = this.panelMain;
				this.m_CommonForm.Show();
			}
			string groupBoxText = wallTypeGroupInfos.GroupName + "：" + wallTypeGroupInfos.LayerTypeList[num2].TypeName;
			this.m_CommonForm.SetGroupBoxText(groupBoxText);
			this.m_CurrentGroupIndex = num;
			this.m_CurrentTypeIndex = num2;
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg != 1125)
			{
				base.DefWndProc(ref m);
				return;
			}
			int index = -1;
			int index2 = -1;
			if (!this.SelectedNodeIndices(ref index, ref index2))
			{
				return;
			}
			string typeNameByLayers = WallTypeManager.GetTypeNameByLayers(this.m_CommonForm.CurrentWallLayerInfo);
			this.m_WallTypeGroupInfoList[index].LayerTypeList[index2].TypeName = typeNameByLayers;
			this.SetNodeText(typeNameByLayers);
		}

		private void SetNodeText(string typeName)
		{
			TreeNode selectedNode = this.treeViewUserSelect.SelectedNode;
			if (selectedNode.Parent == null)
			{
				if (selectedNode.Tag.ToString().CompareTo("") == 0)
				{
					return;
				}
				selectedNode.FirstNode.Text = typeName;
				return;
			}
			else
			{
				if (selectedNode.Parent.Tag.ToString().CompareTo("") == 0)
				{
					return;
				}
				if (selectedNode.Tag.ToString().CompareTo("") == 0)
				{
					return;
				}
				selectedNode.Text = typeName;
				return;
			}
		}

		private bool SelectedNodeIndices(ref int groupIndex, ref int typeIndex)
		{
			TreeNode selectedNode = this.treeViewUserSelect.SelectedNode;
			if (selectedNode.Parent == null)
			{
				string text = selectedNode.Tag.ToString();
				if (text.CompareTo("") == 0)
				{
					return false;
				}
				groupIndex = Convert.ToInt32(text);
				typeIndex = 0;
				return true;
			}
			else
			{
				string text2 = selectedNode.Parent.Tag.ToString();
				if (text2.CompareTo("") == 0)
				{
					return false;
				}
				groupIndex = Convert.ToInt32(text2);
				string text3 = selectedNode.Tag.ToString();
				if (text3.CompareTo("") == 0)
				{
					return false;
				}
				typeIndex = Convert.ToInt32(text3);
				return true;
			}
		}

		private void btnRollback_Click(object sender, EventArgs e)
		{
			this.m_WallTypeGroupInfoList.Clear();
			foreach (WallTypeGroupInfos wallTypeGroupInfos in this.m_OldWallTypeGroupInfoList)
			{
				WallTypeGroupInfos item = wallTypeGroupInfos.DeepClone();
				this.m_WallTypeGroupInfoList.Add(item);
			}
			int num = -1;
			int num2 = -1;
			if (!this.SelectedNodeIndices(ref num, ref num2))
			{
				return;
			}
			List<WallLayerInfos> layerList = this.m_WallTypeGroupInfoList[num].LayerTypeList[num2].LayerList;
			this.m_CommonForm.CurrentWallLayerInfo = layerList;
			this.UpdateUserSelectTreeView(num, num2);
			this.InitializeRemovedWallTypeList();
		}

		private void btnDeleteLayer_Click(object sender, EventArgs e)
		{
			int num = -1;
			int num2 = -1;
			if (!this.SelectedNodeIndices(ref num, ref num2))
			{
				return;
			}
			if (this.m_WallTypeGroupInfoList[num].LayerTypeList.Count < 2)
			{
				return;
			}
			if (this.m_WallTypeGroupInfoList[num].LayerTypeList[num2].HasStructureLayer())
			{
				MessageBox.Show("核心层不能删除", "盈建科提示");
				return;
			}
			this.m_WallTypeGroupInfoList[num].LayerTypeList.RemoveAt(num2);
			this.AddToRemovedWallTypeList(num, num2);
			this.m_CurrentTypeIndex = -1;
			if (num2 == this.m_WallTypeGroupInfoList[num].LayerTypeList.Count)
			{
				this.UpdateUserSelectTreeView(num, this.m_WallTypeGroupInfoList[num].LayerTypeList.Count - 1);
				return;
			}
			this.UpdateUserSelectTreeView(num, num2);
		}

		private void WallTypeEditForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((System.Windows.Forms.Form)sender).DialogResult != DialogResult.OK)
			{
				return;
			}
			List<WallLayerInfos> currentWallLayerInfo = this.m_CommonForm.CurrentWallLayerInfo;
			List<CompoundStructureLayer> list = new List<CompoundStructureLayer>();
			foreach (WallLayerInfos wallLayerInfos in currentWallLayerInfo)
			{
				list.Add(wallLayerInfos.StructureLayer);
			}
			string text = "";
			if (!WallTypeManager.IsValidWithLayers(list, this.m_Doc, ref text))
			{
				MessageBox.Show(text, "盈建科提示");
				e.Cancel = true;
			}
			foreach (WallTypeGroupInfos wallTypeGroupInfos in this.m_WallTypeGroupInfoList)
			{
				foreach (WallTypeLayerInfos wallTypeLayerInfos in wallTypeGroupInfos.LayerTypeList)
				{
					string typeNameByLayers = WallTypeManager.GetTypeNameByLayers(wallTypeLayerInfos.LayerList);
					wallTypeLayerInfos.TypeName = typeNameByLayers;
				}
			}
		}

		private Document m_Doc;

		private CompoundStructureCommonForm m_CommonForm;

		private int m_CurrentGroupIndex = -1;

		private int m_CurrentTypeIndex = -1;

		private List<WallTypeGroupInfos> m_OldWallTypeGroupInfoList = new List<WallTypeGroupInfos>();

		private List<WallTypeGroupInfos> m_WallTypeGroupInfoList = new List<WallTypeGroupInfos>();

		private List<List<int>> m_RemovedWallTypeList = new List<List<int>>();
	}
}
